﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Web.Old_App_Code;

public partial class backstage_article_type_list : System.Web.UI.Page
{
    // Fields
    protected ArticleTypeManager atm = new ArticleTypeManager();

    protected string kw = "";
    protected string order = "sortOrder";
    protected int ot = 0;
    protected int pageCount = 0;
    protected int pageIndex = 1;
    protected int pageSize = 20;
    protected string parentType = "";
    protected string pid = "00000000-0000-0000-0000-000000000000";
    protected PageList pl = new PageList();
    protected PageListManager plm = new PageListManager();
    protected int t = 0;
    protected int total = 0;

    // Methods
    private void ArticleTypeBind()
    {
        try
        {
            if (base.Request.QueryString["p"] != null)
            {
                this.pageIndex = int.Parse(base.Request.QueryString["p"]);
            }
            if (base.Request.QueryString["ps"] != null)
            {
                this.pageSize = Convert.ToInt32(base.Request.QueryString["ps"]);
            }
            if (base.Request.QueryString["or"] != null)
            {
                this.order = base.Request.QueryString["or"];
            }
            if (base.Request.QueryString["ot"] != null)
            {
                this.ot = Convert.ToInt32(base.Request.QueryString["ot"]);
            }
            if (base.Request.QueryString["pid"] != null)
            {
                this.pid = base.Request.QueryString["pid"];
            }
            this.pl.TblName = "tb_articleType";
            this.pl.StrGetFields = "id,typeIdentity,parentId,typeName,sortOrder,isShow,convert(nvarchar(10),createTime,121) as createTime";
            this.pl.FldName = this.order;
            this.pl.PageSize = this.pageSize;
            this.pl.PageIndex = this.pageIndex;
            this.pl.DoCount = 1;
            this.pl.OrderType = this.ot;
            if ((base.Request.QueryString["t"] != null) && (base.Request.QueryString["kw"] != null))
            {
                this.kw = base.Request.QueryString["kw"];
                this.pl.StrWhere = base.Request.QueryString["t"] + " like '%" + this.kw + "%' and parentId='" + this.pid + "' and languageVersion='" + this.Session["language_version"].ToString() + "'";
            }
            else
            {
                this.pl.StrWhere = "parentId='" + this.pid + "' and languageVersion='" + this.Session["language_version"].ToString() + "'";
            }
            this.total = Convert.ToInt32(this.plm.GetPageList(this.pl).Rows[0]["Total"]);
            this.pl.DoCount = 0;
            DataTable pageList = this.plm.GetPageList(this.pl);
            new PageListNo().DelPageSize(pageList, this.total);
            if (pageList.Rows.Count == 0)
            {
                this.btnDelete.Visible = false;
            }
            this.rptArticleType.DataSource = pageList.DefaultView;
            this.rptArticleType.DataBind();
        }
        catch
        {
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        DataTable table = new DataTable();
        string parentId = base.Request.Form["Item"];
        if (parentId.Contains(","))
        {
            Array array = parentId.Split(new char[] { ',' });
            foreach (string str2 in array)
            {
                if (this.atm.GetArticleTypeByParentTypeAndLanguageVersion(str2, this.Session["language_version"].ToString()).Rows.Count == 0)
                {
                    this.atm.DeleteArticleType(str2);
                }
            }
        }
        else if (this.atm.GetArticleTypeByParentTypeAndLanguageVersion(parentId, this.Session["language_version"].ToString()).Rows.Count == 0)
        {
            this.atm.DeleteArticleType(parentId);
        }
        base.Response.Redirect(HttpContext.Current.Request.Url.PathAndQuery);
    }

    protected string DisplayArticleType(object id)
    {
        string str = "";
        try
        {
            if ((((Convert.ToInt32(id) >= 1) && (Convert.ToInt32(id) <= 8)) || (Convert.ToInt32(id) == 20)) || (Convert.ToInt32(id) == 0x15))
            {
                return "display:none;";
            }
            str = "display:block;";
        }
        catch
        {
        }
        return str;
    }

    protected bool DisplayDeleteArticleType(object id)
    {
        bool flag = true;
        try
        {
            if ((((Convert.ToInt32(id) >= 1) && (Convert.ToInt32(id) <= 8)) || (Convert.ToInt32(id) == 20)) || (Convert.ToInt32(id) == 0x15))
            {
                return false;
            }
            flag = true;
        }
        catch
        {
        }
        return flag;
    }

    protected string GetCheckBox(object tId, object id)
    {
        string str = "";
        try
        {
            if ((((Convert.ToInt32(tId) >= 1) && (Convert.ToInt32(tId) <= 8)) || (Convert.ToInt32(tId) == 20)) || (Convert.ToInt32(tId) == 0x15))
            {
                return "";
            }
            str = "<input id=\"cb" + tId.ToString() + "\" name=\"Item\" type=\"checkbox\" onclick=\"ShowDelete();\" value=\"" + id.ToString() + "\" />";
        }
        catch
        {
        }
        return str;
    }

    protected string GetParentType(object pid)
    {
        string str = "";
        DataTable articleTypeById = this.atm.GetArticleTypeById(pid.ToString());
        if (articleTypeById.Rows.Count > 0)
        {
            str = articleTypeById.Rows[0]["typeName"].ToString();
        }
        return str;
    }

    protected override object LoadPageStateFromPersistenceMedium()
    {
        byte[] inArray = ViewStateCompression.DeCompress(Convert.FromBase64String(base.Request.Form.Get("__MSPVSTATE")));
        LosFormatter formatter = new LosFormatter();
        return formatter.Deserialize(Convert.ToBase64String(inArray));
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
        if (!base.IsPostBack)
        {
            if (base.Request.QueryString["t"] != null)
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "sT", "<script type=\"text/javascript\">selectIsExitItem(\"selectType\",\"" + base.Request.QueryString["t"] + "\");</script>");
            }
            if (base.Request.QueryString["ps"] != null)
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "sPs", "<script type=\"text/javascript\">selectIsExitItem(\"selectPageSize\",\"" + base.Request.QueryString["ps"] + "\");</script>");
            }
            if (base.Request.QueryString["pid"] != null)
            {
                if (base.Request.QueryString["pid"] != "00000000-0000-0000-0000-000000000000")
                {
                    DataTable articleTypeById = this.atm.GetArticleTypeById(base.Request.QueryString["pid"]);
                    if (articleTypeById.Rows.Count > 0)
                    {
                        this.parentType = "article_type_list.aspx?pid=" + articleTypeById.Rows[0]["parentId"].ToString();
                    }
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "ssm", "document.getElementById(\"btnParentType\").style.display=\"none\";", true);
                }
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "ssm", "document.getElementById(\"btnParentType\").style.display=\"none\";", true);
            }
            this.btnDelete.Attributes.Add("disabled", "disabled");
            this.ArticleTypeBind();
        }
    }

    protected void rptArticleType_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
        string parentId = e.CommandArgument.ToString();
        if (e.CommandName == "del")
        {
            if (this.atm.GetArticleTypeByParentTypeAndLanguageVersion(parentId, this.Session["language_version"].ToString()).Rows.Count == 0)
            {
                if (this.atm.DeleteArticleType(parentId))
                {
                    base.Response.Redirect(pathAndQuery);
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sd", "<script language=\"javascript\" defer>alert('删除失败！');location.href='" + pathAndQuery + "';</script>");
                }
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "sd", "<script language=\"javascript\" defer>alert('请先删除下级分类信息！');location.href='" + pathAndQuery + "';</script>");
            }
        }
    }

    protected override void SavePageStateToPersistenceMedium(object pViewState)
    {
        LosFormatter formatter = new LosFormatter();
        StringWriter output = new StringWriter();
        formatter.Serialize(output, pViewState);
        string hiddenFieldInitialValue = Convert.ToBase64String(ViewStateCompression.Compress(Convert.FromBase64String(output.ToString())));
        base.ClientScript.RegisterHiddenField("__MSPVSTATE", hiddenFieldInitialValue);
    }
}
